﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussines;
using System.Data.SqlClient;

namespace ownx
{
    /*
    public class clRIGHT
    {
        public int I;
        public string S;
        public clRIGHT(int iI, string iS)
        {
            I = iI;
            S = iS;
        }
    }
    public class clRIGHTS
    {
        public List<clRIGHT> lRIGHTS = new List<clRIGHT>();

        public clRIGHTS()
        {
            clRIGHT i1 = new clRIGHT(1,"aaaa");
            lRIGHTS.Add(i1);

            clRIGHT i2 = new clRIGHT(1, "aaaa");
            lRIGHTS.Add(i2);
        }
    }
    */


    
    public partial class wfRights : System.Web.UI.Page
    {
        clRIGHTS _clRIGHTS;
        clUser _clUser;
        DataSet dsRIGHTS;
        DataSet dsUser;
        int _USR_ID = -1;
        DataSet ds;
        TBLObj blObj = null;

        #region Page load, datainit
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["BL"] != null)
            {
                blObj = (TBLObj)Session["BL"];
            }
            //TBLObj blObj = new TBLObj("Test", Session["CONSTR"].ToString(), (clFIELDINFO_LIST)Session["FIELDINFO"]);
            _USR_ID = Convert.ToInt32(Session["MOD_USR_ID"].ToString());
            _clRIGHTS = new clRIGHTS(blObj, _USR_ID);

            _clUser = new clUser(blObj);

            dsRIGHTS = _clRIGHTS.GetRIGHTS();

            //ha ez az első meghívás
            if (!(Page.IsPostBack))
            {
                DataInit();
            }
        }

        private void DataInit()
        {
            //feltöltöm a detasetet, majd a gridet
            dsRIGHTS = _clRIGHTS.GetRIGHTS();


            gvRights.DataSource = dsRIGHTS;
            gvRights.DataBind();

            //feltötlöm a főnök combot, és kiválasztom a főnököt
            ds = new DataSet();
            ds = _clUser.GetUsers();
            ddlUSR_FONOK.Items.Clear();
            for (int i = 0; i < ds.Tables["_USER"].Rows.Count; ++i)
            {
                ListItem li = new ListItem(ds.Tables["_USER"].Rows[i]["USR_NAME"].ToString(), ds.Tables["_USER"].Rows[i]["USR_ID"].ToString());
                ddlUSR_FONOK.Items.Add(li);
            }

            if (_USR_ID != -1)
            {
                dsUser = _clUser.GetUser(_USR_ID);
                FillEditBoxes();
            }

            
        }
        #endregion


        

        protected void gvRights_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int r = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "VALT")
            {
                if (gvRights.Rows[r].Cells[3].Text != "X")
                    gvRights.Rows[r].Cells[3].Text = "X" ;
                else
                    gvRights.Rows[r].Cells[3].Text = "";
            }
        }

        protected void bVissza_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfUsers.aspx");
        }
        protected bool SaveUser( ref string oErr )
        {
            string iMod;/*EDIT/NEW*/
            
            string name;
            string login_name;
            int fonok;
            bool supervisor;
            string pw;

            if (_USR_ID == -1)
                iMod = "NEW";
            else
                iMod = "EDIT";
                    

            name = tbUSR_NAME.Text;
            login_name = tbUSR_LOGIN_NAME.Text;
            pw = tbPW.Text;

            supervisor = cbSupervisor.Checked;
            if (supervisor)
                fonok = -1;
            else
                fonok = Convert.ToInt32(ddlUSR_FONOK.Items[ddlUSR_FONOK.SelectedIndex].Value);

            if (iMod == "EDIT")
            {  
                
                return (_clUser.UpdateUsers( _USR_ID, name, login_name, pw, fonok, supervisor, ref oErr));               
            }
            else
            {
                _USR_ID = Bussines.Utilities.GenId(blObj.sqlConnection);
                return (_clUser.InsertUsers(_USR_ID , name, login_name, pw, fonok, supervisor, ref oErr));
            }
        }
        protected void bMent_Click(object sender, EventArgs e)
        {
            int iRGH_ID = -1;
            int iRGS_ID = -1;
            int iRIGHT = -1;
            string oErr = "";

            if (SaveUser(ref oErr))
            {
                _clRIGHTS.SetUSR_ID(_USR_ID);
                for (int i = 0; i < gvRights.Rows.Count; ++i)
                {
                    iRGH_ID = Convert.ToInt32(gvRights.Rows[i].Cells[0].Text);
                   // iRGS_ID = Convert.ToInt32(gvRights.Rows[i].Cells[2].Text);
                    if (gvRights.Rows[i].Cells[3].Text != "X")
                        iRIGHT = 0;
                    else
                        iRIGHT = 1;

                    _clRIGHTS.SaveRight(iRGH_ID, iRIGHT, ref oErr);
                }
                lMessage.Text = "";
                Response.Redirect("wfUsers.aspx");
            }
            else
                lMessage.Text = oErr;
            DataInit();
        }
        protected void FillEditBoxes()
        {
            //lépegetés a gridben
            bool supervisor = false;

            tbUSR_NAME.Text = dsUser.Tables["_USER"].Rows[0]["USR_NAME"].ToString();
            tbUSR_LOGIN_NAME.Text = dsUser.Tables["_USER"].Rows[0]["USR_LOGIN_NAME"].ToString();
            tbPW.Text = dsUser.Tables["_USER"].Rows[0]["USR_PW"].ToString();
            supervisor = (dsUser.Tables["_USER"].Rows[0]["USR_SUPERVISOR"].ToString() == "1");
            cbSupervisor.Checked = supervisor;
            //   ddlUSR_FONOK.Visible = (!(supervisor));
            for (int i = 0; i < ddlUSR_FONOK.Items.Count; ++i)
            {
                ddlUSR_FONOK.Items[i].Enabled = (!(ddlUSR_FONOK.Items[i].Value == dsUser.Tables["_USER"].Rows[0]["USR_ID"].ToString()));
                if (ddlUSR_FONOK.Items[i].Value == dsUser.Tables["_USER"].Rows[0]["USR_FONOK"].ToString())
                    ddlUSR_FONOK.SelectedIndex = i;
            }
            lMessage.Text = "";
        }

        protected void bVisszaFooldalra_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfDefault.aspx");
        }
    }
}
